from sqlalchemy import Column, Integer, Date, Enum
from database import Base
from sqlalchemy.orm import relationship
from sqlalchemy import ForeignKey
from datetime import date

class Payment(Base):
    __tablename__ = "payment"

    ID = Column(Integer, primary_key=True, index=True, autoincrement=True)
    patient_ID = Column(Integer, ForeignKey("patient.ID"))
    con_date = Column(Date, default=date.today()) # todo:这个未必合适
    expense = Column(Integer)
    type = Column(Enum('BOOK', 'MEDI'))
    pay_y_n = Column(Enum('Y', 'N', name='payment_status'))
    ref_ID= Column(Integer)

    # Define relationship with patient
    patient = relationship("Patient", back_populates="payments")

    def to_dict(self):
        return {
            "ID": self.ID,
            "patient_ID": self.patient_ID,
            "con_date": self.con_date.isoformat() if self.con_date else None,
            "expense": self.expense,
            "type": self.type,
            "pay_y_n": self.pay_y_n
        }

    @classmethod
    def from_dict(cls, data: dict) -> "Payment":
        valid_keys = set(cls.__table__.columns.keys())
        return cls(**{k: v for k, v in data.items() if k in valid_keys})